//
//  HRWelcomeViewController.swift
//  weiBo
//
//  Created by helong on 16/7/12.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit

class HRWelcomeViewController: UIViewController {

    //懒加载背景
    private lazy var backgroudImageView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "ad_background"))
        return imgView
    }()
    //懒加载头像
    private lazy var headImageView: UIImageView = {
        //设置展位图片
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
               //设置圆角
        imgView.sd_setImageWithURL(NSURL(string: (HRUserAccountInfos.sharedUserAccount.accountInfo?.avatar_large)!), placeholderImage: UIImage(named: "avatar_default_big"))
        imgView.layer.cornerRadius = (UIImage(named: "avatar_default_big")?.size.width)! * 0.5
        imgView.layer.masksToBounds = true
        return imgView
    }()
    //懒加载label
    private lazy var welcomeLabel: UILabel = {
        let lbl = UILabel()
        lbl.text = HRUserAccountInfos.sharedUserAccount.accountInfo?.name
        lbl.alpha = 0
        return lbl
    }()
    
    override func loadView() {
        view = backgroudImageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
        
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()//开启欢迎动画
    }
     // MARK: - 添加控件并设置约束
    private func setUpUI(){
        view.addSubview(headImageView)//头像
        view.addSubview(welcomeLabel)//欢迎词
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSizeMake(85, 85))
            make.centerX.equalTo(view)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(495)
            make.centerX.equalTo(view)
        }


    }
     // MARK: - 欢迎页面的动画
    private func startAnimation(){
        //更新约束
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
//            make.size.equalTo(CGSizeMake(40, 40))
        }
        welcomeLabel.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(195)
        }
        //设置动画
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //重新布局view子控件
                self.view.layoutIfNeeded()
                }, completion: { (_) -> Void in

                    UIView .animateWithDuration(1, animations: { () -> Void in
                        //头像停止运动之后动画显示文字
                        self.welcomeLabel.alpha = 1
                        }, completion: { (_) -> Void in
                            NSNotificationCenter.defaultCenter().postNotificationName(switchControllerNotificationName, object: nil)
                    })
            })
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }
    

}
